package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.RoleFunctionDAO;
import com.junzhiit.nengji.shop.model.RoleFunction;

@Repository
public class RoleFunctionDAOImpl extends AbstractBaseDAO<RoleFunction> implements RoleFunctionDAO {

	@Override
	public int getTotalCountByRole(String roleId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from role_function");
		sql.append(" where is_delete=?");
		sql.append(" and role_id=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(roleId);

		return super.queryForInt(sql.toString(), param);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public boolean deleteByRoleId(String roleId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" delete from role_function where role_id=?");

		StatementParameter param = new StatementParameter();
		param.setString(roleId);

		return super.updateForBoolean(sql.toString(), param);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public boolean deleteByFunctionId(String functionId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" delete from role_function where function_id=?");

		StatementParameter param = new StatementParameter();
		param.setString(functionId);

		return super.updateForBoolean(sql.toString(), param);
	}

	@Override
	public List<RoleFunction> getRoleFunction(String roleId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from role_function");
		sql.append(" where role_id=?");
		sql.append(" and is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setString(roleId);
		param.setInt(IsDelete.NO.toIntValue());

		return super.queryForList(sql.toString(), RoleFunction.class, param);
	}
}
